package net.ib.happy2v.contact.authenticate;

import net.ib.happy2v.R;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AuthenticatorActivity extends AccountAuthenticatorActivity 
			implements OnClickListener {
	
	private EditText mEditTextId;
	private EditText mEditTextPasswd;
	private Button mBtnLogin;
	private Button mBtnCancel;
	private TextWatcher mIdWatcher;
	private TextWatcher mPasswordWatcher;
	
	private boolean mIdEnable, mPasswordEnable;
	
	private AccountManager accountManager;
	private String mAccountType;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.dialog_add_account);
		
		Bundle bundle = getIntent().getExtras();
		mAccountType = bundle.getString(AccountManager.KEY_ACCOUNT_TYPE);
		
		mPasswordEnable = mIdEnable = false;
		
		mBtnLogin = (Button)findViewById(R.id.Button_login);
		mBtnCancel = (Button)findViewById(R.id.Button_cancel);
		mEditTextId = (EditText)findViewById(R.id.EditText_id);
		mEditTextPasswd = (EditText)findViewById(R.id.EditText_password);
		
		mBtnLogin.setOnClickListener(this);
		mBtnCancel.setOnClickListener(this);
		mBtnLogin.setClickable(false);
		
		accountManager = AccountManager.get(this);
		setWatcher();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mEditTextId.addTextChangedListener(mIdWatcher);
		mEditTextPasswd.addTextChangedListener(mPasswordWatcher);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mEditTextId.removeTextChangedListener(mIdWatcher);
		mEditTextPasswd.removeTextChangedListener(mPasswordWatcher);
	}
	
	private void setWatcher(){
		mIdWatcher = new TextWatcher() {
			public void afterTextChanged(Editable s) {}
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (TextUtils.isEmpty(s)) {
					mIdEnable = false;
				} else {
					mIdEnable = true;
				}
				if(mIdEnable && mPasswordEnable){
					mBtnLogin.setClickable(true);
				}else{
					mBtnLogin.setClickable(false);
				}
			}
		};
		
		mPasswordWatcher = new TextWatcher() {
			public void afterTextChanged(Editable s) {}
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (TextUtils.isEmpty(s)){
					mPasswordEnable = false;
				} else {
					mPasswordEnable = true;
				}
				if(mIdEnable && mPasswordEnable){
					mBtnLogin.setClickable(true);
				}else{
					mBtnLogin.setClickable(false);
				}
			}
		};
	}
	 
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.Button_login:
				doLogin();
				break;
			case R.id.Button_cancel:
				doCancel();
				break;
			default:
				break;
		}
	}
	
	private void doLogin(){
		final String idString = mEditTextId.getText().toString();
		final String passwdString = mEditTextPasswd.getText().toString();
		final Account account = new Account(idString, mAccountType);
		if(accountManager.addAccountExplicitly(account, passwdString, null)){
			final Intent intent = new Intent();
			intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, idString);
			intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, mAccountType);
			
			setAccountAuthenticatorResult(intent.getExtras());
			setResult(RESULT_OK, intent);
			finish();
		}else{
			doCancel();
		}
	}
	
	private void doCancel(){
		setResult(RESULT_CANCELED);
		finish();
	}
}
